import { CACHE_USERINFO } from "@/global/constants"
import { localCache } from "@/utils/cacheData"
import { messageReminder } from "@/utils/notificationHint"
import { createRouter, createWebHashHistory } from "vue-router"

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      redirect: "/main/home"
    },
    //登录
    {
      path: "/login",
      component: () => import("../views/login-register/Login.vue")
    },
    {
      path: "/register",
      component: () => import("../views/login-register/Register.vue")
    },
    {
      path: "/main",
      component: () => import("../views/main/main.vue"),
      meta: { requiresAuth: true }, // 设置路由元信息，表示需要登录验证，
      children: [
        // 1 项目概览 project-overview
        {
          path: "home",
          name: "Home",
          component: () => import("../views/main/Home/Home.vue"),
          children: [],
          meta: {
            activeIndex: "1"
          }
        },
        {
          path: "databases",
          name: "Databases",
          component: () => import("../views/main/Databases/Databases.vue"),
          children: [],
          meta: {
            activeIndex: "2"
          }
        },
        {
          path: "accommodationParticular",
          name: "AccommodationParticular",
          component: () => import("../views/main/Databases/accommodationParticular.vue"),
          children: []
        },
        {
          path: "myAccom",
          name: "MyAccom",
          component: () => import("../views/main/MyAccom/MyAccom.vue"),
          children: [],
          meta: {
            activeIndex: "4"
          }
        },
        {
          path: "personalCenter",
          name: "PersonalCenter",
          component: () => import("../views/main/PersonalCenter/personalCenter.vue"),
          children: [],
          meta: {
            activeIndex: "5"
          }
        },
        {
          path: "release",
          name: "Release",
          component: () => import("../views/main/Release/releaseAccommodation.vue"),
          children: [],
          meta: {
            activeIndex: "3"
          }
        }
      ]
    },
    {
      path: "/:pathMatch(.*)",
      component: () => import("../views/404-NotFound/404-NotFound.vue")
    }
  ],
  // 每次切换路由页面滚回到顶部
  scrollBehavior() {
    return { top: 0 }
  }
})

router.beforeEach((to, from, next) => {
  const userInfo = localCache.getCache(CACHE_USERINFO)

  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // 检查路由元信息
    if (!userInfo) {
      // 如果本地存储中不存在用户信息，则重定向到登录页面
      next({
        path: "/login",
        query: { redirect: to.fullPath } // 记录用户本次尝试访问的页面路径
      })
    } else {
      judgmentAuthority(to, from, next)
    }
  } else {
    // 不需要登录验证的页面，直接放行
    next()
  }
})

function judgmentAuthority(to, from, next) {
  const userInfo = localCache.getCache(CACHE_USERINFO)
  let isMember = false
  userInfo &&
    userInfo.authority.forEach((element) => {
      if (element.name == "Member") {
        isMember = true
      }
    })
  if (to.name === "Release") {
    if (!isMember) {
      // 如果用户权限不是 Member，则重定向到其他页面，比如首页
      messageReminder({
        message: "对不起，权限不足！",
        type: "error"
      })
    } else {
      // 用户权限为 Admin，允许进入 /audit 页面
      next()
    }
  } else {
    // 对于其他页面，直接放行
    next()
  }
}

export default router
